#!/bin/bash

#Desc:init system
#Author:xttcqw
#Create:8-19
#Update:2020-9-8
#Usage:$0 $1/hostname $2/host ip
#Release:v2.0

#check network link
checknetwork() {
	echo "正在检查网络。。。"
	ping -c 3 114.114.114.114 &>/dev/null
	if [ $? -eq 0 ]; then
		echo "网络已经准备好！！！"
	else
		echo "请先检查网络"
		exit
	fi
}

#set hostname
sethostname() {
	hostnamectl set-hostname $1
	echo "主机名称$1设置成功!"
}

#close firewall
closefirewall() {
	systemctl disable firewalld && systemctl stop firewalld
	firewall-cmd --state &>/dev/null
	if [ $? -eq 252 ]; then
		echo "Firewall设置关闭成功！"
	else
		echo "Firewall关闭失败！请检查原因！！！"
	fi
}

#close selinux
closeselinux() {
	setenforce 1 &>/dev/null
	sed -i "/^SELINUX/c\SELINUX=disabled" /etc/selinux/config
	selinuxstatus=$(sestatus | grep "status" | awk '{print $3}')
	if [ "$selinuxstatus" == "disabled" ]; then
		echo "selinux已设置不可用，重启电脑后生效！"
	else

		echo "selinux设置失败请检查"
	fi
}

#close Network Manager
closeNetworkManager() {
	systemctl disable NetworkManager && systemctl stop NetworkManager
	NManagerstatus=$(systemctl status NetworkManager | awk '/Active:/{print $2}')
	if [ "$NManagerstatus" == "inactive" ]; then
		echo "关闭NetworkManager成功"
	else
		echo "关闭NetworkManager失败"
	fi
}

#set host ip
sethostip() {
	cat >/etc/sysconfig/network-scripts/ifcfg-ens33 <<EOF
TYPE=Ethernet
BOOTPROTO=none
IPADDR=$1
PREFIX=24
GATEWAY=192.168.1.2
DNS1=119.29.29.29
NAME=ens33
DEVICE=ens33
ONBOOT=yes
EOF
	echo "网络IP设置成功，重启网络服务后生效"
}

#set epel by aliyun
setepel() {
	echo "开始配置阿里epel源。。。"
	which wget &>/dev/null
	if [ $? -eq 0 ]; then
		wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
	else
		curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
	fi
	yum clean all &>/dev/null
	yum makecache
	echo "阿里云epel源配置完成"
}
#set yum repo
setyum_al() {
	echo "开始配置阿里YUM源。。。"
	mkdir -p /etc/yum.repos.d/back
	mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/back
	which wget &>/dev/null
	if [ $? -eq 0 ]; then
		wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
	else
		curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
	fi
	yum clean all &>/dev/null
	yum makecache
	echo "阿里云YUM源配置成功"
}
setyum_tx() {
	echo "开始配置腾讯YUM源。。。"
	mkdir -p /etc/yum.repos.d/back
	mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/back
	which wget &>/dev/null
	if [ $? -eq 0 ]; then
		wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base.repo
	else
		curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base.repo
	fi
	yum clean all &>/dev/null
	yum makecache
	echo "腾讯YUM源配置成功"
}

installtools() {
	echo "安装常用工具 net-tools vim bash-completion ntpdate ntp wget "
	yum -y install net-tools vim bash-completion ntpdate ntp &>/dev/null
	echo "安装完毕"
}

#lock in time
locktime() {
	echo "开始配置时钟服务。。。"
	ntpdate ntp.aliyun.com
	sed -ri.bak '/^server [1-3]/d' /etc/ntp.conf
	sed -ri "/^server/c\server ntp.aliyun.com" /etc/ntp.conf
	systemctl start ntpd
	echo "时钟配置成功"
}

display() {
	cat <<EOF
===========================================
=======	系统初始化工具======================
	=======	1)设置主机名
	=======	2)关闭防火墙
	=======	3)关闭selinux
	=======	4)关闭NetworkManager
	=======	5)设置IP
	=======	6)配置yun镜像源(默认腾讯源)
	=======	7)安装常用工具
	=======	8)同步服务器时间
	=======	9)一键初始化
	=======	0)重启计算机
	=======	q|Q)退出
	======= e|E)epel源
=======	请根据自己的需要，设置初始化
==========================================
EOF
}
#man function
man() {
	checknetwork
	while :; do
		display
		read -p "请输入选择项[1|2|3|4|5|6|7|8|9|0|e|E|q|Q]:" choise
		case $choise in
		1)
			clear
			sethostname $1
			;;
		2)
			clear
			closefirewall
			;;
		3)
			clear
			closeselinux
			;;
		4)
			clear
			closeNetworkManager
			;;
		5)
			clear
			sethostip $2
			;;
		6)
			clear
			setyum_tx
			;;
		7)
			clear
			installtools
			;;
		8)
			clear
			locktime
			;;
		9)
			clear
			sethostname $1
			closefirewall
			closeselinux
			closeNetworkManager
			sethostip $2
			setyum_tx
			installtools
			locktime
			;;
		0)
			reboot
			;;
		Q | q)
			echo "退出系统初始化工具"
			break
			;;
		e | E)
			setepel
			;;
		*)
			echo "你的输入有误"
			;;
		esac
	done
}

#call man
man $1 $2
